<template lang="">
    <div>
        <!-- 面包屑导航区域 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>商品列表</el-breadcrumb-item>
        </el-breadcrumb>

        <!-- 卡片视图区域 -->
        <el-card>
            <!-- 搜索与添加区域 -->
            <!-- gutter 指定每一栏之间的间隔
                span	栅格占据的列数
            -->
            <el-row :gutter="20">
                <el-col :span="8">
                    <!-- 
                        clearable   可得到一个可清空的输入框
                        clear	在点击由 clearable 属性生成的清空按钮时触发
                        搜索时 根据queryInfo.query 作为搜索的参数进行搜索
                     -->
                    <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getGoodsList">
                        <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
                        <!-- getGoodsList方法中,使用了axios库发送HTTP GET请求 需要携带参数 这里携带的是data中的queryinfo query也在其中 后端就会按照接收到的query参数查询符合要求的数据 -->
                    </el-input>
                </el-col>
                <el-col :span="4">
                    <el-button type="primary" @click="goAddpage">添加商品</el-button>
                </el-col>
            </el-row>

            <!-- table 表格区域 -->
            <el-table :data="goodsList" border stripe>
                <el-table-column type="index"></el-table-column>
                <el-table-column label="商品名称" prop="goods_name"></el-table-column>
                <el-table-column label="商品价格(元)" prop="goods_price" width="95px"></el-table-column>
                <el-table-column label="商品重量" prop="goods_weight" width="70px"></el-table-column>
                <el-table-column label="创建时间" prop="add_time" width="140px">
                    <template v-slot="scope">
                        {{scope.row.add_time | dataFormat}}
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="130px">
                    <template v-slot="scope">
                        <el-button size="mini" icon="el-icon-edit" type="primary"
                            @click="showGoodsEditDialog(scope.row)"></el-button>
                        <el-button size="mini" icon="el-icon-delete" type="danger"
                            @click="removeById(scope.row.goods_id)"></el-button>
                    </template>
                </el-table-column>


            </el-table>
            <!-- 分页区域 -->
            <!-- 
                page-size	    每页显示条目个数
                current-page	当前页数
                page-sizes	    每页显示个数选择器的选项设置
                total	        总条目数
                size-change	    pageSize 改变时会触发	
                current-change	currentPage 改变时会触发
             -->
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                :current-page="queryInfo.pagenum" :page-sizes="[5, 10, 20, 30]" :page-size="queryInfo.pagesize"
                layout="total, sizes, prev, pager, next, jumper" :total="total">
            </el-pagination>

        </el-card>


        <!-- 编辑商品对话框 -->
        <el-dialog title="编辑商品信息" :visible.sync="editGoodsDialogVisible" width="50%" @close="editGoodsDialogClosed">
            <el-form :model="editGoodsForm" ref="editGoodsFormRef" :rules="editGoodsFormRules" label-width="100px">
                <el-form-item label="商品名称" prop="goods_name">
                    <el-input v-model="editGoodsForm.goods_name"></el-input>
                </el-form-item>
                <el-form-item label="商品价格" prop="goods_price">
                    <el-input v-model="editGoodsForm.goods_price"></el-input>
                </el-form-item>
                <el-form-item label="商品重量" prop="goods_weight">
                    <el-input v-model="editGoodsForm.goods_weight"></el-input>
                </el-form-item>
                <el-form-item label="商品数量" prop="goods_number">
                    <el-input v-model="editGoodsForm.goods_number"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editGoodsDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="editGoods">确 定</el-button>
            </span>
        </el-dialog>


    </div>
</template>
<script>
    export default {
        name: 'List',
        data() {
            return {
                // 查询参数对象
                queryInfo: {
                    query: '',  // 用于搜索的关键字
                    pagenum: 1, // 当前页码值
                    pagesize: 10 // 每页显示多少条数据
                },
                total: 0,

                goodsList: [],
                // 控制添加商品对话框的显示与隐藏
                addGoodsDialogVisible: false,
                // 控制编辑商品对话框的显示与隐藏
                editGoodsDialogVisible: false,

                editGoodsForm: {
                    goods_cat: '0',
                    goods_name: '',
                    goods_price: 0,
                    goods_weight: 0,
                    goods_number: 0
                },

                editGoodsFormRules: {
                    goods_name: [
                        { required: true, message: '请输入商品名称', trigger: 'blur' }
                    ],
                    goods_price: [
                        { required: true, message: '请输入商品价格', trigger: 'blur' }
                    ],
                    goods_weight: [
                        { required: true, message: '请输入商品重量', trigger: 'blur' }
                    ],
                    goods_number: [
                        { required: true, message: '请输入商品重量', trigger: 'blur' }
                    ]
                }

            }
        },
        computed: {

        },
        created() {
            this.getGoodsList()
        },
        methods: {
            // 根据分页获取对应的商品列表
            async getGoodsList() {
                const { data: res } = await this.$http.get('goods', { params: this.queryInfo })
                // console.log(res);
                if (res.meta.status !== 200) return this.$message.error('获取商品列表失败')
                this.goodsList = res.data.goods
                this.total = res.data.total
                // console.log(this.goodsList);
            },
            // 监听 pagesize 改变的事件
            handleSizeChange(newSize) {
                this.queryInfo.pagesize = newSize
                this.getGoodsList()
            },
            // 监听 pagenum 改变的事件
            handleCurrentChange(newPage) {
                this.queryInfo.pagenum = newPage
                this.getGoodsList()
            },

            // 根据id删除对应商品
            removeById(id) {
                this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(async () => {
                    const { data: res } = await this.$http.delete('goods/' + id)
                    // console.log(res);
                    if (res.meta.status !== 200) return this.$message.error('删除商品失败')
                    this.getGoodsList()
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },

            goAddpage() {
                this.$router.push('/goods/add')
            },
            // 修改商品
            async showGoodsEditDialog(row) {
                // console.log(row);
                const { data: res } = await this.$http.get('goods/' + row.goods_id)
                if (res.meta.status !== 200) return this.$message.error('获取商品信息失败')
                // console.log(res);
                this.editGoodsForm = res.data
                // console.log(this.editGoodsForm);
                this.editGoodsDialogVisible = true
            },
            // 提交修改
            async editGoods() {
                const { data: res } = await this.$http.put(`goods/${this.editGoodsForm.goods_id}`, {
                    goods_cat: this.editGoodsForm.goods_cat,    // 必要
                    goods_name: this.editGoodsForm.goods_name,
                    goods_price: this.editGoodsForm.goods_price,
                    goods_number: this.editGoodsForm.goods_number,
                    goods_weight: this.editGoodsForm.goods_weight
                })

                if (res.meta.status !== 200) return this.$message.error('修改商品失败')
                // console.log(res);
                this.$message.success('修改商品成功')
                this.editGoodsDialogVisible = false
                this.getGoodsList()
            },

            // 监听修改商品对话框的关闭事件
            editGoodsDialogClosed() {
                this.$refs.editGoodsFormRef.resetFields()
            },
        }
    }
</script>
<style scoped>
    .el-table {
        margin-top: 20px;
    }
</style>